import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

//!解决登录的时候报错(这是到当前位置的冗余导航)
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

function checkAuthor(next, url) {
  let user = JSON.parse(sessionStorage.getItem('user')) || null;
  if (user) {
    //判断数组是否存在某一个元素,如果存在返回true，否则返回false
    if (!user.menus_url.some((item) => item === url)) {
      next('/index/home')
    } else {
      next()
    }
  } else {
    next('/login')
  }
}

const routes = [
  {
    path: '/login',
    component: () => import('../pages/Login/login.vue'),
    name: '登 录',
    meta: { title: '登录页面' }   //路由元信息
  },
  {
    path: '/index',
    component: () => import('../pages/Index/index.vue'),
    children: [  //二级路由规则
      {
        path: 'home',
        component: () => import('../views/Home/home.vue'),
        name: '首页',
        meta: { title: '首页' }
      },
      {
        path: 'menu',
        component: () => import('../views/menu/menu.vue'),
        name: '菜单管理',
        meta: { title: '菜单管理' },  //路由元信息
        // 路由独享守卫
        beforeEnter(to, from, next) {
          checkAuthor(next, 'menu')
        }
      },
      {
        path: 'role',
        component: () => import('../views/role/role.vue'),
        name: '角色管理',
        meta: { title: '角色管理' },
        beforeEnter(to, from, next) {
          checkAuthor(next, 'role')
        }
      },
      {
        path: 'member',
        component: () => import('../views/member/member.vue'),
        name: '管理员管理',
        meta: { title: '管理员管理' },
        beforeEnter(to, from, next) {
          checkAuthor(next, 'member')
        }
      },
      {
        path: 'cate',
        component: () => import('../views/cate/cate.vue'),
        name: '商品分类',
        meta: { title: '商品分类' },
        beforeEnter(to, from, next) {
          checkAuthor(next, 'cate')
        }
      },
      {
        path: 'specs',
        component: () => import('../views/specs/specs.vue'),
        name: '商品规格',
        meta: { title: '商品规格' },
        beforeEnter(to, from, next) {
          checkAuthor(next, 'specs')
        }
      },
      {
        path: 'goods',
        component: () => import('../views/goods/goods.vue'),
        name: '商品管理',
        meta: { title: '商品管理' },
        beforeEnter(to, from, next) {
          checkAuthor(next, 'goods')
        }
      },
      {
        path: 'user',
        component: () => import('../views/user/user.vue'),
        name: '会员管理',
        meta: { title: '会员管理' },
        beforeEnter(to, from, next) {
          checkAuthor(next, 'user')
        }
      },
      {
        path: 'banner',
        component: () => import('../views/banner/banner.vue'),
        name: '轮播图',
        meta: { title: '轮播图' },
        beforeEnter(to, from, next) {
          checkAuthor(next, 'banner')
        }
      },
      {
        path: 'seckill',
        component: () => import('../views/seckill/seckill.vue'),
        name: '秒杀活动',
        meta: { title: '秒杀活动' },
        beforeEnter(to, from, next) {
          checkAuthor(next, 'seckill')
        }
      },
      {
        path: '/index',
        redirect: '/index/home'
      }
    ]

  },
  {
    path: '*',
    redirect: '/login'
  }
]

const router = new VueRouter({
  routes
})

//全局守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    let user = JSON.parse(sessionStorage.getItem('user')) || null;
    if (user) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
